/*******************************************************************************
*  file    : qloggercontext.h
*  created : 02.05.2013
*  author  : 
*******************************************************************************/

#ifndef QLOGGERCONTEXT_H
#define QLOGGERCONTEXT_H

#include "qloggerconf.hpp"
#include <QTextStream>

class QLogger;

class QLOGGER_DECL QLoggerContext
{
    Q_DISABLE_COPY(QLoggerContext)
public:
    QLoggerContext(): line_(0),file_(0),
        function_(0),category_(LevelNone),logger_(0),stream_(0) {}
    QLoggerContext(const char *fileName, int lineNumber, const char *functionName)
        : line_(lineNumber), file_(fileName), function_(functionName),
          category_(LevelNone), logger_(0), stream_(0) {}
    ~QLoggerContext();
    QLoggerContext& operator<<(const QString&);
    QLoggerContext& operator<<(int i);
    QLoggerContext& operator<<(unsigned int i);
    QLoggerContext& operator<<(double d);
    template<typename T>
    QLoggerContext& operator<<(T t);
    QLoggerContext& debug    (QLogger* l, const QString& msg = "");
    QLoggerContext& info     (QLogger* l, const QString& msg = "");
    QLoggerContext& warn     (QLogger* l, const QString& msg = "");
    QLoggerContext& error    (QLogger* l, const QString& msg = "");
    QLoggerContext& fatal    (QLogger* l, const QString& msg = "");
    QLoggerContext& critical (QLogger* l, const QString& msg = "");
    QLoggerContext& exception(QLogger* l, const QString& msg = "");
private:
    int         line_;
    const char* file_;
    const char* function_;
    QLoggerLevel category_;
    QLogger*    logger_;
    QString     msg_;
    QTextStream* stream_;
};

template<typename T>
QLoggerContext& QLoggerContext::operator<<(T t)
{
    if(stream_ == 0)
        stream_ = new QTextStream(&msg_);

    (*stream_) << t;
    return *this;
}

#endif // QLOGGERCONTEXT_H
